\section{Comparison}
\label{sec:comp}
In this section I present a comparison between formatting integers in C++ and in C\#.
As explained in \subref{sub:streamManip} C++ stream manipulators and member functions can be used to alter the way a stream formats different types of objects.
In C\# I generally use one of two ways to format integers depending on how much control is required.
The first being the usage of the format string used as input to the \texttt{WriteLine} method, which is present at the \texttt{StreamWriter} class and at \texttt{Console} class.
The other method is to convert the integer to a string and then printing the string, effectively doing the formatting before passing the variable to the stream.

To print the number ``42'' in hexadecimal using format string on a stream is done as follows:
\begin{lstlisting}[style=code, float=false]
StreamWriter writer = new StreamWriter(Console.OpenStandardOutput()); // Initialize writer to print to the console
writer.AutoFlush = true; // Ensure that buffer is flushed
writer.WriteLine("{0,10:X}", 42); // Prints '        A2'
\end{lstlisting}
This syntax is actually quite similar to the way strings are formatted in C when using \texttt{printf} for instance.
However, C\# does offer some type safety -- it is not necessary to specify in the format string which type each argument is.
I do find this syntax quite obscure; the brackets indicate that a parameter is to be formatted here, the ``0'' indicates which parameter (zero index), the ``10'' after the comma specifies the length (the same as width in C++) and finally the ``X'' tells to format the parameter in hexadecimal~\cite{writeLine}.
For simply outputting a couple of variables without any special formatting this might be acceptable as the format string could simply be \texttt{\"Parameters: \{0\} \{1\} \{2\} ...\"}.
I would like to add that instead of initializing a \texttt{StreamWriter}, the \texttt{Console} class could have been used directly:
\begin{lstlisting}[style=code, float=false]
Console.WriteLine("{0,10:X}", 42); // Prints '        A2'
\end{lstlisting}

Another way, which gives a little more control is, as mentioned, to format an integer as a string and then simply giving the string to the stream.
This can be done as follows:
\begin{lstlisting}[style=code, float=false]
writer.WriteLine(Convert.ToString(42, 2).PadLeft(10, '0')); // Prints '0000101010'
\end{lstlisting}
Notice that here a binary base is used and that a specific padding character is chosen.
To the best of my knowledge this is not possible using the previous method.
We see here a chaining of method calls, but instead of chaining on the stream it self, as in C++, the chaining is here done on the string which the integer is converted to.

Both these methods of formatting integers requires work at every call to the \texttt{WriteLine} method, where as in C++ options are simply set on a stream and no more work is needed.
It is my opinion that it is very powerful to be able to specify options on a stream such that the programmer does not have to copy-paste code such as \texttt{Convert.ToString(42, 2).PadLeft(10, '0')} every time an integer is to be output.
It is easy to think a scenario where, all of a sudden, the length/width of integer output might be changed from 10 to e.g. 12, in which case a single point of change would of course be preferable -- assuming the change should apply for all integers being formatted.
On the other hand it might be attractive to format integers locally; e.g. in one place we want them formatted with fixed length/width and in hexadecimal, another place we want them simply as ``regular'' decimal integers.

I have found that a \texttt{FormatProvider} can be specified for a \texttt{StreamWriter}, which handles standard way which formatting is handled (including integers)~\cite{formatProvider}.
If using this, it is still possible to format in integer as a string and passing the string (second method), when some specific format, which differs from that of the \texttt{FormatProvider}, is preferred.
I have, however, never used \texttt{FormatProvider}s myself.